package org.lottery.activity;

import org.lottery.entity.CardGraphicView;
import org.lottery.entity.ImageAdapter;
import org.lottery.entity.UserSettings;
import org.lottery.util.ImageUtil;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class LotteryActivity extends Activity {

    private UserSettings us;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);
        // init settings
        us = initSettings();
        GridView gridView = (GridView) findViewById(R.id.grid_view);
        gridView.setAdapter(new ImageAdapter(this, us));
        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                // flip card - get result - reward/goodluck
                flipCard(v);
            }

        });
    }

    private UserSettings initSettings() {

        UserSettings us = new UserSettings();
        try {
            SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
            String rewardPath = sharedPref.getString(SettingsActivity.SETTINGS_REWARD_PATH, "");
            String goodluckPath = sharedPref.getString(SettingsActivity.SETTINGS_GOODLUCK_PATH, "");
            String openPath = sharedPref.getString(SettingsActivity.SETTINGS_OPEN_PATH, "");
            String quantity = sharedPref.getString(SettingsActivity.SETTINGS_REWARD_QUANTITY, "");
            us.setQuantity(Integer.parseInt(quantity));
            us.setResources(getResources());
            us.setRewardPath(rewardPath);
            us.setGoodluckPath(goodluckPath);
            us.setOpenPath(openPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return us;
    }

    protected void flipCard(View v) {

        // flip card
        CardGraphicView cardBingo = ((CardGraphicView) v);
        Drawable resultImage = null;
        if (cardBingo.isBingo()) {
            resultImage = ImageUtil.initRewardPicture(us.getRewardPath(), us.getResources());
            if(resultImage!=null){
            	cardBingo.setImageDrawable(resultImage);
            }else{
            	cardBingo.setImageResource(R.drawable.bingo);
            }
        } else {
            resultImage = ImageUtil.initRewardPicture(us.getGoodluckPath(), us.getResources());
            if(resultImage!=null){
            	cardBingo.setImageDrawable(resultImage);
            }else{
            	cardBingo.setImageResource(R.drawable.chuc_ban_may_man_lan_sau);
            }
        }
    }
}
